from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session
from database import get_db
from schemas.admin_schemas import AdminCreate, AdminUpdate
from services.auth_service import require_user

from config import *  # 导入所有配置
from models.user_models import *

from services.user_service import get_all_users,get_user,create_user,delete_user,update_user

router = APIRouter()

# 查询获取所有管理员信息
@router.get("/admins")
def get_admins(db: Session = Depends(get_db),
                    user=Depends(require_user(UserType.ADMIN, UserType.ADMIN))):
    return get_all_users(UserType.ADMIN, db)

# 查询单个管理员的信息
@router.get("/admins/{admin_id}")
def get_admin(admin_id:int,db: Session = Depends(get_db),user=Depends(require_user(UserType.ADMIN))):
    return get_user(UserType.ADMIN,db,admin_id)


# 创建管理员账户
@router.post("/admins")
def create_admin(admin: AdminCreate,
                      db: Session = Depends(get_db),
                      user=Depends(require_user(UserType.ADMIN))):
    return create_user(UserType.ADMIN, db, admin)


# 删除管理员账户
@router.delete("/admins/{admin_id}")
def delete_admin(admin_id: int,
                      db: Session = Depends(get_db),
                      user=Depends(require_user(UserType.ADMIN))):
    return delete_user(UserType.ADMIN,db,admin_id)


# 更新管理员信息
@router.put("/admins/{admin_id}")
def update_admin(admin_id: int,
                      admin_update: AdminUpdate,
                      db: Session = Depends(get_db),
                      user=Depends(require_user(UserType.ADMIN))):
    return update_user(UserType.ADMIN, db, admin_update, admin_id)